import { createRouter, createWebHistory } from 'vue-router'
 import homeView from '@/views/home/index.vue'
 import shouView from '@/views/home/home.vue'
  import fenView from '@/views/home/fen.vue'
   import cartView from '@/views/home/cart.vue'
    import myView from '@/views/home/my.vue'
     import searchView from '@/views/search/index.vue'
      import detailView from '@/views/detail/index.vue'
       import goodsView from '@/views/goods/index.vue'
        import orderView from '@/views/order/index.vue'
         import registerView from '@/views/register/index.vue'
          import loginView from '@/views/login/index.vue'
           import myorderView from '@/views/myorder/index.vue'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/home',
      component: homeView,
      children: [
        {
          path: 'home',
          component: shouView,
        },
        {
          path: 'fen',
          component: fenView,
        },
        {
          path: 'cart',
          component: cartView,
        },
        {
          path: 'my',
          component: myView,
        },
      ]
    },
    {
      path: '/search',
      component: searchView,
    },
    {
      path: '/detail',
      component: detailView,
    },
    {
      path: '/detail/:goods_id',
      component: detailView,
    },
    {
      path: '/goods/:cat_name',
      component: goodsView,
    },
    {
      path: '/order/:orderId',
      component: orderView,
    },
    {
      path: '/register',
      component: registerView,
    },
    {
      path: '/login',
      component: loginView,
    },
    {
      path: '/myorder',
      component: myorderView,
    },
{ path: '/', redirect: '/home/home' }
  ],
})
router.beforeEach((to, from,next) => {
 if(to.fullPath=='/home/cart'||to.fullPath=='/home/my'){
  let token=localStorage.getItem('token')
  if(token){
    next()
  }
  else{
    next('/login')
  }
 }
 else{
  next()
 }
})
export default router
